import { Injectable } from '@angular/core';
import { Http } from "@angular/http";
@Injectable()
export class UserService {

  isLogin:boolean;
  userName;
  icon;
  email;
  signature;
  url = 'http://www.zhengshangfeng.xin/';
  constructor(public http:Http) {
    this.isLogin = window.localStorage.getItem("isLogin")==='true';
    !this.isLogin?window.localStorage.setItem("userName",'匿名'):this.getData();
   }
   uploadImg(img){
     return this.http.post(this.url+'upload.php',img);
   }
  getData(){
    this.userName= window.localStorage.getItem("userName");
    this.icon= window.localStorage.getItem("icon");
    this.email= window.localStorage.getItem("email");
    this.signature= window.localStorage.getItem("signature");
    this.isLogin = window.localStorage.getItem("isLogin")==='true';
  }
  setData(data){
    window.localStorage.setItem("userName",data.userName);
    window.localStorage.setItem("icon",data.icon);
    window.localStorage.setItem("email",data.email);
    window.localStorage.setItem("signature",data.signature);
    window.localStorage.setItem('userId',data.userId);
    this.getData();
  }
  setIsLogin(judge:string){
    this.isLogin = judge==='true';
    window.localStorage.setItem("isLogin",judge);
  }
  login(user){
    let cred = "userName=" + user.userName + "&passWord=" + user.passWord;
    return this.http.get(this.url + 'login.php?' + cred);
  }
  logout(){
    this.isLogin = false;
    window.localStorage.clear();
    
  }
  signup(user){
    let cred = "userName=" + user.userName + "&passWord=" + user.passWord + "&email=" + user.email;
    return this.http.get(this.url + 'signup.php?' + cred);
  }
  change(user){
    
    let cred = 
    "userName=" + user.userName + 
    "&passWord=" + user.passWord +
    "&email=" + user.email +
    "&signature=" + user.signature +
    "&icon=" + user.icon +
    "&isChanged=" + user.isChanged+
    "&newName=" +  user.newName
    ;
    return this.http.get(this.url + 'change.php?' + cred);
  }

  getUserInfo(){
    let cred = 
    "userId=" + window.localStorage.getItem('userId')
    return this.http.get(this.url + 'profile.php?' + cred);
  }
}
